package com.learn.mail.controller;

import com.learn.mail.enums.MailTemplateType;
import com.learn.mail.service.SendEmailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private SendEmailServiceImpl sendEmailService;

    private static final String path = "templates/mail_code.html";

    @GetMapping("/send")
    public String sendEmail() {
        try {
            Map<String, Object> model = new HashMap<>();
            model.put("name", "John Doe");

            sendEmailService.sendTemplateMail("448341911@qq.com", "Test Email", path, model);
            return "Email sent successfully!";
        } catch (Exception e) {
            e.printStackTrace();
            return "Error sending email: " + e.getMessage();
        }
    }

    @GetMapping("/mailAsync")
    public String mailAsync() {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("username", "张三");
            params.put("code", "123456");
            params.put("timeout", 5);
            sendEmailService.sendHtmlMailAsync("448341911@qq.com", "验证码", MailTemplateType.VERIFY_CODE, params);

            return "Email sent successfully!";
        } catch (Exception e) {
            e.printStackTrace();
            return "Error sending email: " + e.getMessage();
        }
    }

    @PostMapping("/sendBatch")
    public String sendBatch(@RequestBody List<String> recipients) {
        Map<String, Object> params = new HashMap<>();
        params.put("username", "用户");
        params.put("code", "123456");
        params.put("timeout", 5);
        sendEmailService.sendBatchEmails(recipients, "验证码提醒", "mail-template.html", params);
        return "发送完成";
    }
}
